package com.dirox.jira.notes.action;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.builder.JqlQueryBuilder;
import com.atlassian.jira.security.PermissionManager;
import com.atlassian.jira.security.Permissions;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.web.action.JiraWebActionSupport;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.query.Query;
import com.dirox.jira.notes.entities.Note;
import com.dirox.jira.notes.service.NoteService;
import static com.google.common.base.Preconditions.checkNotNull;
import org.apache.commons.lang.StringUtils;
import java.util.List;
import java.util.SortedSet;


public class GetNoteIssueTabPanel extends JiraWebActionSupport {
	private static final long serialVersionUID = 1L;
	private IssueManager issueManager;
	private NoteService noteService;
	private SearchService searchService;
	private PermissionManager permissionManager;
	private UserUtil userUtil;
	private GroupManager groupManager;
	private int hasNote;
	private String issueKey;
	private List<Note> notes;
	private List<Note> memos;

	protected String doExecute() throws Exception {
		if (!validateInput()) {
			return ERROR;
		}

		com.dirox.jira.notes.entities.User owner = noteService
			                                   .currentUser();

		this.notes = noteService.getNotes(this.issueKey, owner);
		this.memos = noteService.getMemos(this.issueKey, owner);
		hasNote = ((this.notes.size() > 0) || (this.memos.size() > 0))
		          ? 1
		          : 0;

		return SUCCESS;
	}

	/**
	 * Validate the input parameters
	 *
	 * @return true if validating input success, else return false
	 */
	private boolean validateInput() {
		// check if issue empty or not exist
		if (StringUtils.isBlank(issueKey)
			    || (issueManager.getIssueObject(issueKey) == null)) {
			return false;
		}

		return true;
	}

	public User getCurrentUser() {
		return getLoggedInUser();
	}

	public int getHasLastNote() {
		return this.hasNote;
	}

	public List<Note> getNotes() {
		return this.notes;
	}

	public List<Note> getMemos() {
		return this.memos;
	}

	public List<Issue> getIssues() {
		Query query = JqlQueryBuilder.newClauseBuilder()
		                             .buildQuery();

		try {
			List<Issue> issues = searchService.search(
				getLoggedInUser(),
				query,
				PagerFilter.getUnlimitedFilter())
			                                  .getIssues();

			for (Issue issue : issues) {
				if (!permissionManager.hasPermission(
						Permissions.BROWSE,
						issue,
						getLoggedInUser())) {
					issues.remove(issue);
				}
			}

			return issues;
		} catch (SearchException e) {
			e.printStackTrace();

			return null;
		}
	}

	public SortedSet<User> getUsers() {
		return userUtil.getAllUsersInGroups(groupManager.getAllGroups());
	}

	public String getIssueKey() {
		return issueKey;
	}

	public void setIssueKey(String issueKey) {
		this.issueKey = issueKey;
	}

	public void setIssueManager(IssueManager issueManager) {
		this.issueManager = checkNotNull(issueManager);
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = checkNotNull(searchService);
	}

	public void setNoteService(NoteService noteService) {
		this.noteService = checkNotNull(noteService);
	}

	public void setPermissionManager(PermissionManager permissionManager) {
		this.permissionManager = permissionManager;
	}

	public void setUserUtil(UserUtil userUtil) {
		this.userUtil = checkNotNull(userUtil);
	}

	public void setGroupManager(GroupManager groupManager) {
		this.groupManager = groupManager;
	}
}
